package com.example.myapplication.mgreendao.pojo;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.github.yuweiguocn.library.greendao.MigrationHelper;
import com.test.greendao.DaoMaster;
import com.test.greendao.StudentGreenDao;
import com.test.greendao.UserGreenDao;

import org.greenrobot.greendao.database.Database;

public class MySqlLiteOpenHelper extends DaoMaster.OpenHelper {
    public MySqlLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
        super(context, name, factory);
    }
    //借助MigrationHelper类升级即可
    @Override
    public void onUpgrade(Database db, int oldVersion, int newVersion) {

        MigrationHelper.migrate(
                db,
                new MigrationHelper.ReCreateAllTableListener() {
                    @Override
                    public void onCreateAllTables(Database db, boolean ifNotExists) {
                        DaoMaster.createAllTables(db, ifNotExists);
                    }
                    @Override
                    public void onDropAllTables(Database db, boolean ifExists) {
                        DaoMaster.dropAllTables(db, ifExists);
                    }
                }, UserGreenDao.class, StudentGreenDao.class
        );

//        switch (newVersion){
//            case 3:
//                //传入新表  里面执行的还是执行语句,以及写好的建表语句
//                /*
//                db.execSQL("CREATE TABLE " + constraint + "\"STUDENT_GREEN\" (" + //
//                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
//                "\"NAME\" TEXT);"); // 1: name
//                * */
//                DaoMaster.createAllTables(db,true);
//                //加入新字段  注意实体类中对于数字类要使用包装类，如int为Intege
//                try {
//                    db.execSQL("ALTER TABLE 'user_green' ADD 'AVATAR' TEXT;");
//                } catch (SQLException e) {
//                    e.printStackTrace();
//                }
//                break;
//        }

//        //根据旧版本号
//        switch (oldVersion){
//            case 1:...
//            case 2:
//        }
    }
}
